package cn.kgc.house.controller;

import cn.kgc.house.pojo.HouseInfo;
import cn.kgc.house.pojo.HouseType;
import cn.kgc.house.service.HouseTypeService;
import cn.kgc.house.service.HouserInfoService;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;


@Controller
public class HouseController {
    @Resource
    HouserInfoService houserInfoService;
    @Resource
    HouseTypeService houseTypeService;
    @RequestMapping("house/select")
    public String selectAll(Model model,
     @RequestParam(value = "typeid",required = false) Integer typeid,
    @RequestParam(value = "pageNo",required = false,defaultValue = "1") Integer pageNo
    ){
        PageInfo<HouseInfo> pageInfo = houserInfoService.selectAll(typeid, pageNo);
        List<HouseType> houseTypes = houseTypeService.selectAll();
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("typeid",typeid);
        model.addAttribute("pageNo",pageNo);
        model.addAttribute("houseTypes",houseTypes);
        return "index";
    }
    @RequestMapping("house/toadd")
    public String toadd(Model model){
       /* List<HouseType> houseTypes = houseTypeService.selectAll();
        model.addAttribute("houseTypes",houseTypes);*/
        return "add";
    }
    @RequestMapping("house/add")
    public String add(HouseInfo houseInfo){
        houseInfo.setPublishdate(new Date());
         houserInfoService.add(houseInfo);
        return "redirect:/house/select";
    }

    @RequestMapping("house/update")
    public String update(@RequestParam HouseInfo houseInfo,Model model){
        houserInfoService.update(houseInfo);
        return "index";
    }
    @RequestMapping("house/delete")
    public String delete(@RequestParam Integer houseId){
        houserInfoService.delete(houseId);
        return "redirect:/index";
    }
}
